package ar.com.sectorwine.controllers;

import java.util.List;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import ar.com.sectorwine.dto.ParametroDTO;
import ar.com.sectorwine.dto.ParametrosDTOMapper;
import ar.com.sectorwine.entities.Parametro;
import ar.com.sectorwine.services.ParametrosService;

/**
 * Handles requests for the application home page.
 */
@Controller
@Transactional(propagation = Propagation.REQUIRES_NEW)
public class ParametrosController {

	@Autowired
	private ParametrosService parametrosService;
	
	@Autowired
	private ParametrosDTOMapper parametrosDTOMapper;

	
	private static final Logger logger = LoggerFactory
			.getLogger(ParametrosController.class);

	@RequestMapping(value = "parametros_listar", method = RequestMethod.GET)
	public ModelAndView lista(Locale locale, Model model) {

		logger.info("Listar Parametros Controller");

		ModelAndView mav = new ModelAndView("parametros/parametros_listar");
		
		List<Parametro> parametros = parametrosService.findAll();
	
		mav.addObject("parametrosList", parametros);

		return mav;

	}

	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "parametros_modificar", method = RequestMethod.GET)
	public ModelAndView abrirModificarParametros(Locale locale, @RequestParam String nombre,
			Model model) {

		logger.info("Welcome home! the client locale is " + locale.toString());

		ModelAndView mav = new ModelAndView("parametros/parametros_modificar");

		Parametro parametro = parametrosService.findParametro(nombre);
		ParametroDTO parametroDTO = parametrosDTOMapper.obtenerDTODesdeObjetoDelDominio(parametro);
		mav.addObject("parametro", parametroDTO );
		mav.addObject("valores", parametroDTO.getValor());
		mav.addObject("nuevoParametro", "");

		return mav;
	}

	@RequestMapping(value = "parametros_modificar", method = RequestMethod.POST)
	public ModelAndView modificar(
			@ModelAttribute("parametro") ParametroDTO parametroDTO,
			@RequestParam String nuevoParametro,
			BindingResult result, SessionStatus status) {

		logger.info("controller");
		
		Parametro parametro = parametrosDTOMapper.obtenerObjetoDelDominioDesdeDTO(parametroDTO);
	
		if(nuevoParametro != null && !nuevoParametro.isEmpty()){
			parametro.getValor().add(nuevoParametro);
		}
		
		parametro = parametrosService.modificarParametro(parametro);
		
		ModelAndView mav = new ModelAndView("parametros/parametros_modificar");
		
		parametroDTO = parametrosDTOMapper.obtenerDTODesdeObjetoDelDominio(parametro);
		
		mav.addObject("parametro", parametroDTO );
		mav.addObject("valores", parametroDTO.getValor());
		
		return mav;

	}

	@RequestMapping(value = "parametro_baja", method = RequestMethod.POST)
	public ModelAndView darBajaParametro(Locale locale, @RequestParam String nombre,
			Model model) {

		logger.info("controller");

		ModelAndView mav = new ModelAndView("home");
		parametrosService.eliminar(nombre);
		return mav;
	}
	
}